﻿#include "SmtUiUtils.h"
#include "qmap.h"
#include <SMTGUI.h>
#include "qdebug.h"
#include "qmessagebox.h"
#include <QtCore/qstring.h>
#include "HttpUtils.h"
#include "HwUtils.h"
#include <SmtGraphicsDef.h>

using namespace opt_smt;

extern HttpUtils httpUtils;

SmtUiUtils::SmtUiUtils()
{
}

void SmtUiUtils::setOperatorGraphics(SMTOperatorGraphics *graphicOperator)
{

}

void SmtUiUtils::handleInitUI(SMTVariableArray & m_paraInput, int index, SMTButtonBox * pWidget)
{
	SMTVariant val;
	m_paraInput[index].getValue(&val);
	int dvalue;
	val.toInt(&dvalue);

	pWidget->blockSignals(true);
	pWidget->setCurrentIndex(dvalue);
	pWidget->blockSignals(false);
}

void SmtUiUtils::handleInitUI(SMTVariableArray & m_paraInput, int index, SMTSpinBox * pWidget)
{
	SMTVariant val;
	m_paraInput[index].getValue(&val);
	int dvalue;
	val.toInt(&dvalue);

	pWidget->blockSignals(true);
	pWidget->setValue(dvalue);
	pWidget->blockSignals(false);
}

void SmtUiUtils::handleInitUI(SMTVariableArray & m_paraInput, int index, SMTSwitchButton * pWidget)
{
	SMTVariant val;
	m_paraInput[index].getValue(&val);
	bool bvalue;
	val.toBool(&bvalue);

	pWidget->blockSignals(true);
	pWidget->setChecked(bvalue);
	pWidget->blockSignals(false);
}

void SmtUiUtils::handleInitUI(SMTOperator *m_opr, QComboBox *pWidget, int index)
{
	SMTVariableArray *m_paraInput;
	m_opr->GetInputVariables(&m_paraInput);
	SMTVariant val;
	(*m_paraInput)[index].getValue(&val);
	SMTString dvalue;
	val.toString(&dvalue);
	QString checkvalue = dvalue.toLocal8Bit().constData();
	pWidget->blockSignals(true);
	pWidget->setCurrentText(dvalue.toLocal8Bit().constData());
	pWidget->blockSignals(false);
}

bool SmtUiUtils::getAiServiceStatus(SMTOperator* opr, int idx)
{
	SMTVariableArray* m_paraInput;
	SMTVariant tmpVariant;
	bool aiServiceStatus = false;
	opr->GetInputVariables(&m_paraInput);
	(*m_paraInput)[idx].getValue(&tmpVariant);
	tmpVariant.toBool(&aiServiceStatus);
	return aiServiceStatus;
	}

void SmtUiUtils::setAiServiceStatus(SMTOperator* opr, int idx, bool ok)
{
	SMTVariableArray* m_paraInput;
	SMTVariant tmpVariant;
	opr->GetInputVariables(&m_paraInput);
	tmpVariant.fromBool(ok);
	(*m_paraInput)[idx].setValue(tmpVariant);
}

void SmtUiUtils::getAiUrl(QString sceneName,
	QString productName,
	SMTOperator* pOpr,
	int idxVarInOfAiServiceStatus,
	QLineEdit* lineAiUrl,
	QLineEdit* lineTaskName,
	QLineEdit* lineProjectName,
	QLabel* lblStatusLight)
{
	qInfo() << "+++++++ start to get aiUrl +++++++";
	QString token = httpUtils.getToken();;
	QString baseUrl = httpUtils.getHostUrl();
	QString serialNumber = httpUtils.getSerialNumber();
	if (!util::HwUtils::checkAiUrlReqParams(serialNumber, baseUrl, token, productName))
		return;

	lineAiUrl->setText("");
	lineTaskName->setText("");
	lineProjectName->setText("");
	QueryAiUrlRes queryRes;
	QueryAiUrlReq req = {productName, sceneName};
	int ret = httpUtils.queryAiUrl(req, queryRes);
	if (ret == -1)
	{
		qInfo() << "get AiUrl, ret:" << ret << ", need to check whether params is completed.";
		setAiServiceStatus(pOpr, idxVarInOfAiServiceStatus, false);
		util::HwUtils::setLED(lblStatusLight, 1, 16);
		return;
	}
	qInfo() << "query AiUrl ret:" << ret;
	if (ret != 0 && ret != 200)
	{
		setAiServiceStatus(pOpr, idxVarInOfAiServiceStatus, false);
		util::HwUtils::setLED(lblStatusLight, 1, 16);
		QMessageBox::critical(NULL,
			QStringLiteral("错误"),
			QStringLiteral("服务端错误(%1) \n错误代码:%2 , 错误信息: %3 \n请联系管理员解决！").arg(QString::number(ret), QString::number(queryRes.errorCode), queryRes.descMsg));
		return;
	}
	lineAiUrl->setText(queryRes.ConnectService);
	lineTaskName->setText(queryRes.TaskName);
	lineProjectName->setText(queryRes.ProjectName);

	if (queryRes.ProjectStatus == "online")
	{
		setAiServiceStatus(pOpr, idxVarInOfAiServiceStatus, true);
		util::HwUtils::setLED(lblStatusLight, 2, 16);
	}
	else
	{
		setAiServiceStatus(pOpr, idxVarInOfAiServiceStatus, false);
		util::HwUtils::setLED(lblStatusLight, 1, 16);
		QMessageBox::warning(NULL, QStringLiteral("信息"), QStringLiteral("获取 %1 对应得推理服务URL失败!").arg(productName));
	}
	qInfo() << "+++++++ end to get AiUrl  +++++++" << endl;
}

void SmtUiUtils::fixAiUrl(QString sceneName, 
	QString productName, 
	SMTOperator* pOpr, 
	int idxVarInOfAiServiceStatus, 
	QLineEdit* lineAiUrl, 
	QLineEdit* lineTaskName, 
	QLineEdit* lineProjectName, 
	QLabel* lblStatusLight)
{
	qInfo() << "+++++++ start to get Fix AiUrl +++++++";
	QString token = httpUtils.getToken();;
	QString baseUrl = httpUtils.getHostUrl();
	QString serialNumber = httpUtils.getSerialNumber();
	if (!util::HwUtils::checkAiUrlReqParams(serialNumber, baseUrl, token, productName))
		return;
	QueryAiUrlRes queryRes;
	QueryAiUrlReq req = {productName, sceneName};
	int ret = httpUtils.deployAiInstance(req, queryRes);
	qInfo() << "Fix AiUrl ret:" << ret;
	if (ret == -1)
	{
		qCritical() << "Fix AiUrl, ret:" << ret << ", need to check whether params is completed.";
		setAiServiceStatus(pOpr, idxVarInOfAiServiceStatus, false);
		util::HwUtils::setLED(lblStatusLight, 1, 16);
		return;
	}

	if (ret != 0 && ret != 200)
	{
		setAiServiceStatus(pOpr, idxVarInOfAiServiceStatus, false);
		util::HwUtils::setLED(lblStatusLight, 1, 16);
		QMessageBox::critical(NULL,
			QStringLiteral("错误"),
			QStringLiteral("服务端错误(%1) \n错误代码:%2 , 错误信息: %3 \n请联系管理员解决！")
			.arg(QString::number(ret), QString::number(queryRes.errorCode), queryRes.descMsg));
		return;
	}

	lineAiUrl->setText(queryRes.ConnectService);
	lineTaskName->setText(queryRes.TaskName);
	lineProjectName->setText(queryRes.ProjectName);

	if (queryRes.ProjectStatus == "online")
	{
		setAiServiceStatus(pOpr, idxVarInOfAiServiceStatus, true);
		util::HwUtils::setLED(lblStatusLight, 2, 16);
		QMessageBox::information(NULL, QStringLiteral("信息"), QStringLiteral("修复 %1 推理服务成功!").arg(productName));
	}
	else if (queryRes.ProjectStatus == "deploying" || queryRes.ProjectStatus == "terminating") {
		util::HwUtils::setLED(lblStatusLight, 3, 16);
		QMessageBox::warning(NULL, QStringLiteral("信息"), QStringLiteral("正在修复 %1 推理服务, 请等待15秒后再次获取推理URL!").arg(productName));
	}
	else
	{
		setAiServiceStatus(pOpr, idxVarInOfAiServiceStatus, false);
		util::HwUtils::setLED(lblStatusLight, 1, 16);
		QMessageBox::critical(NULL, QStringLiteral("信息"), QStringLiteral("修复 %1 推理服务失败!").arg(productName));
	}

	qInfo() << "+++++++ end to get Fix AiUrl  +++++++";
}

void SmtUiUtils::drawShapeElem(SMTOperatorGraphics& m_graphics, int index, bool okFlag, SMTShapeArray& shapeArray, SMTShape::SMTShapeType smtShapeType, char ok_r, char ok_g, char ok_b, char ok_alpha) 
{
	SMTGraphicElement elem(smtShapeType);
	m_graphics.getElement(index, &elem);
	if (okFlag)
	{
		elem.setPenColor(COLOR_GREEN_OPAQUE);
	}
	else
	{
		elem.setPenColor(COLOR_RED_OPAQUE);
	}
	elem.setShapeArray(shapeArray);
	m_graphics.replaceElement(index, elem);
}

void SmtUiUtils::drawShapeElem(SMTOperatorGraphics& m_graphics, int index, bool okFlag, SMTShapeArray& shapeArray, SMTShape::SMTShapeType smtShapeType)
{
	this->drawShapeElem(m_graphics, index, okFlag, shapeArray, smtShapeType,COLOR_GREEN_OPAQUE);
}

void SmtUiUtils::drawRegionElem(SMTOperatorGraphics& m_graphics, int index, bool okFlag, SMTShapeArray& shapeArray, char ok_r, char ok_g, char ok_b, char alpha)
{
	SMTGraphicElement elem(SMTShape::SMT_SHAPE_REGION);
	m_graphics.getElement(index, &elem);
	if (okFlag)
	{
		elem.setBrushColor(ok_r, ok_g, ok_b, alpha);
	}
	else
	{
		elem.setBrushColor(COLOR_RED, alpha);
	}
	elem.setShapeArray(shapeArray);
	m_graphics.replaceElement(index, elem);
}

void SmtUiUtils::drawRegionElem(SMTOperatorGraphics& m_graphics, int index, bool okFlag, SMTShapeArray& shapeArray,char alpha)
{
	this->drawRegionElem(m_graphics, index, okFlag, shapeArray, COLOR_GREEN, alpha);
}

void SmtUiUtils::drawLabelElem(SMTOperatorGraphics& m_graphics, int index, bool okFlag, SMTPointArray& topLeftPointArray, SMTStringArray& labelNameArray,char ok_r,char ok_g, char ok_b, char ok_alpha)
{
	SMTShapeArray fontShapeArray;
	if (topLeftPointArray.length() != 0 && labelNameArray.length() != 0)
		for (int i = 0; i < topLeftPointArray.length(); i++)
		{
			SMTShape fontshape;
			SMTFont font;
			SMTPoint topLeftPoint;
			font.setSize(6);
			font.setBold(false);
			font.setUnderline(false);
			font.setItalic(false);
			fontshape.fromText(topLeftPointArray[i], labelNameArray[i], font);
			fontShapeArray.push_back(fontshape);
		}
	SMTGraphicElement elem(SMTShape::SMT_SHAPE_TEXT);
	m_graphics.getElement(index, &elem);
	if (okFlag)
	{
		elem.setPenColor(ok_r,ok_g,ok_b,ok_alpha);
	}
	else
	{
		elem.setPenColor(COLOR_RED_OPAQUE);
	}
	elem.setShapeArray(fontShapeArray);
	m_graphics.replaceElement(index, elem);
}

void SmtUiUtils::drawLabelElem(SMTOperatorGraphics& m_graphics, int index, bool okFlag, SMTPointArray& topLeftPointArray, SMTStringArray& labelNameArray)
{
	this->drawLabelElem(m_graphics, index, okFlag, topLeftPointArray, labelNameArray, COLOR_GREEN_OPAQUE);
}

void SmtUiUtils::clearGraphicElem(SMTOperatorGraphics& m_graphics, int index)
{
	SMTShapeArray shapeArray;
	SMTGraphicElement elem(SMTShape::SMT_SHAPE_RECT1);
	m_graphics.getElement(index, &elem);
	elem.setShapeArray(shapeArray);
	m_graphics.replaceElement(index, elem);
}


void SmtUiUtils::getImageDataInputFromOpr(SMTOperator *m_opr)
{
#ifdef TESTALGORITHM
	SMTImage mOrignBackgroundImage;
	GET_IMAGEWND->getBackground(&mOrignBackgroundImage);
	SMTVariableArray *inParams;
	m_opr->getDataInput(&inParams);
	(*inParams)[0].setValue(mOrignBackgroundImage);
#endif	
}


SmtUiUtils::~SmtUiUtils()
{

}
